#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "message.pb.h"
#include "base.pb.h"
#include "user.pb.h"
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/message_service", "关注的服务名称");

ServiceChannel::ChannelPtr channel;

// void range_test(const std::string &chat_session_id, 
//                 const std::string &stime,
//                 const std::string &etime) {
//     chat_system::GetHistoryMsgReq req;
//     chat_system::GetHistoryMsgRsp rsp;
//     req.set_chat_session_id(chat_session_id);
//     req.set_start_time(boost::posix_time::to_time_t(boost::posix_time::time_from_string(stime)));
//     req.set_over_time(boost::posix_time::to_time_t(boost::posix_time::time_from_string(etime)));
//     brpc::Controller cntl;
//     chat_system::MsgStorageService_Stub stub(channel.get());
//     stub.GetHistoryMsg(&cntl, &req, &rsp, nullptr);
//     ASSERT_TRUE(!cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     for(auto& msg: rsp.msg_list()) {
//         LOG_DEBUG("收到历史消息: {}", msg.ShortDebugString());
//         LOG_DEBUG("--------------------------------------");
//     }
// }


// void RecentTest(const std::string &chat_session_id, 
//                 const int64_t size) {
//     chat_system::GetRecentMsgReq req;
//     chat_system::GetRecentMsgRsp rsp;
//     req.set_chat_session_id(chat_session_id);
//     req.set_msg_count(size);
//     brpc::Controller cntl;
//     chat_system::MsgStorageService_Stub stub(channel.get());
//     stub.GetRecentMsg(&cntl, &req, &rsp, nullptr);
//     ASSERT_TRUE(!cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     for(auto& msg: rsp.msg_list()) {
//         LOG_DEBUG("收到最近消息: {}", msg.ShortDebugString());
//         LOG_DEBUG("--------------------------------------");
//     }
// }


void SearchTest(const std::string &chat_session_id, const std::string &keyword) {
    chat_system::MsgSearchReq req;
    chat_system::MsgSearchRsp rsp;
    req.set_chat_session_id(chat_session_id);
    req.set_search_key(keyword);
    brpc::Controller cntl;
    chat_system::MsgStorageService_Stub stub(channel.get());
    stub.MsgSearch(&cntl, &req, &rsp, nullptr);
    ASSERT_TRUE(!cntl.Failed());
    ASSERT_TRUE(rsp.success());
    LOG_DEBUG("搜索到消息数: {}", rsp.msg_list_size());
    for(auto& msg: rsp.msg_list()) {
        LOG_DEBUG("收到搜索消息: {}", msg.ShortDebugString());
        LOG_DEBUG("--------------------------------------");
    }
}
int main(int argc, char* argv[]) {
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    google::ParseCommandLineFlags(&argc, &argv, true);
    testing::InitGoogleTest();
    ServiceManager::ptr service_manager = std::make_shared<ServiceManager>();
    service_manager->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    //获取用户服务信道管理对象
    auto channel_manager = service_manager->choose(FLAGS_transmite_service);
    if(channel_manager == nullptr) {
        LOG_DEBUG("获取文件服务信道失败");
        return -1;
    }
    // 获取用户服务信道
    channel = channel_manager->choose();
    if(channel == nullptr) {
        LOG_DEBUG("获取用户服务信道失败");
        return -1;
    }
    // range_test("7651-7470d73d-0000", "2025-08-27 11:53:54", "2025-08-27 11:54:54");
    // RecentTest("7651-7470d73d-0000", 10);
    // SearchTest("会话ID1", "盖浇");
    SearchTest("7651-7470d73d-0000", "在");
    return 0;
}